package com.shigu.test;

import com.opentae.data.mall.beans.MoOrder;
import com.opentae.data.mall.beans.MoPassagewayAccount;
import com.opentae.data.mall.examples.MoPassagewayAccountExample;
import com.opentae.data.mall.interfaces.MoOrderMapper;
import com.opentae.data.mall.interfaces.MoPassagewayAccountMapper;
import com.shigu.main4.common.util.MoneyUtil;
import com.shigu.passageway.Passageway;
import com.shigu.passageway.bo.RegistBO;
import com.shigu.passageway.exceptions.UnpayException;
import com.shigu.passageway.exceptions.YeepayException;
import com.shigu.passageway.processes.UnpayPassageway;
import com.shigu.passageway.processes.YeepayPassageway;
import com.unpay.skb.requests.RegistCertificatesRequest;
import com.unpay.skb.responses.RegistCertificatesResponse;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ActiveAgent extends BaseSpringTest{

    @Autowired
    MoPassagewayAccountMapper moPassagewayAccountMapper;
    Long passagewayId=2L;

    @Autowired
    MoOrderMapper moOrderMapper;

    @Resource(name = "yeepayPassageway")
    Passageway passageway;


    @Test
    public void profitTest() throws UnpayException, YeepayException {
        MoOrder order=moOrderMapper.selectByPrimaryKey(2006);
        YeepayPassageway p= (YeepayPassageway) passageway;
        p.toMakeProfit(806L,order);
    }

    @Test
    public void activeAll() throws UnpayException {
        MoPassagewayAccountExample example=new MoPassagewayAccountExample();
        example.createCriteria().andPassagewayIdEqualTo(1L).andUserIdEqualTo(872L);
        List<MoPassagewayAccount> accounts=moPassagewayAccountMapper.selectByExample(example);
        for(MoPassagewayAccount account:accounts){
            if (account.getAccountId()<=0) {
                continue;
            }
            example.clear();
            example.createCriteria().andPassagewayIdEqualTo(2L).andUserIdEqualTo(account.getUserId());
            if (moPassagewayAccountMapper.countByExample(example)>0) {
                continue;
            }
            Double rate=Double.valueOf(account.getRate())-0.001;
            RegistBO bo=new RegistBO();
            bo.setRealName(account.getRealName());
            bo.setIdCard(account.getBodyNumber());
            bo.setBankName(account.getBankName());
            bo.setBankLocation(account.getBankLocation());
            bo.setBankAccountNumber(account.getBankNumber());
            bo.setTelephone(account.getTelephone());
            bo.setAreaCode(account.getAreaCode());
            bo.setUserId(account.getUserId());
            RegistCertificatesRequest request=new RegistCertificatesRequest();
            SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
            DecimalFormat df=new DecimalFormat("000");
            String subMchId=sdf.format(new Date())+df.format(bo.getUserId()%1000);
            request.setSubMchId(subMchId);
            request.setSubMerName(bo.getRealName());
            request.setSubMerAddr(bo.getBankLocation());
            request.setIdNo(bo.getIdCard());
            request.setPhoneNo(bo.getTelephone());
            request.setSettAcctName(bo.getRealName());
            request.setSettAcct(bo.getBankAccountNumber());
            request.setMerCommisionValue(MoneyUtil.formatMoney(rate*100));
//            request.setTxMerCommisionValue(rate.toString());
            request.setTxMerFee("2");
            RegistCertificatesResponse response=request.exec();
            if (!response.isSuccess()) {
                System.out.println(bo.getRealName());
                System.out.println(response.getMsg());
                continue;
//                throw new UnpayException(response.getMsg());
            }
            MoPassagewayAccount upaccount=new MoPassagewayAccount();
            upaccount.setTelephone(bo.getTelephone());
            upaccount.setAreaCode(bo.getAreaCode());
            upaccount.setAccountCode(subMchId);
            upaccount.setRealName(bo.getRealName());
            upaccount.setBankUser(bo.getRealName());
            upaccount.setRate(rate.toString());
            upaccount.setPassagewayId(passagewayId);
            upaccount.setUserId(bo.getUserId());
            upaccount.setBankLocation(bo.getBankLocation());
            upaccount.setBankName(bo.getBankName());
            upaccount.setBankNumber(bo.getBankAccountNumber());
            upaccount.setBodyNumber(bo.getIdCard());
            upaccount.setUsedStatus(false);
            moPassagewayAccountMapper.insertSelective(upaccount);
        }
    }
}
